//
//  PMSplitCtrl.cpp
//
//  Copyright (C) 2008 Visual.Wei
//  Author: Wei Shuai <cpuwolf@sina.com>
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
//
#include "PMSplitCtrl.h"
#include "PMListView.h"
#include "PMTabCtrl.h"

extern PMListView *p_pmlv;
extern PMTabCtrl * p_pmtc;

PMSplitCtrl::PMSplitCtrl(HWND hWnd,DWORD height):PMControl()
{
	RECT rect;
	GetClientRect(hWnd,&rect);
	m_height=height;
	g_hwnd=hWnd;
	m_bmousedown=FALSE;
	CopyRect(&WndRect,&rect);
	WndRect.top=(rect.bottom-rect.top)/2;
	WndRect.bottom=WndRect.top+(m_height/2);
	WndRect.top=WndRect.top-(m_height/2);

	Update();
}

DWORD PMSplitCtrl::ReSize()
{
	RECT rect;
	GetClientRect(g_hwnd,&rect);
	WndRect.left=rect.left;
	WndRect.right=rect.right;
	return TRUE;
}

BOOL PMSplitCtrl::CheckMouse(DWORD x,DWORD y)
{
	RECT rect;
	GetClientRect(g_hwnd,&rect);
	if((y<rect.top+30)||(y>rect.bottom-30))
		return FALSE;

	return TRUE;
}
void PMSplitCtrl::MouseDown(DWORD fkey,DWORD x,DWORD y)
{
	POINT pt; 
	HDC hdc;
	RECT rect;


	if((y>=WndRect.top)&&(y<=WndRect.bottom))
	{
		m_bmousedown=TRUE;
		GetClientRect(g_hwnd,&rect);
		pt.x=rect.left;
		pt.y=rect.top;
		ClientToScreen( g_hwnd, &pt );
		//click in the window
		hdc=GetDC(g_hwnd);
		clickoffset=(y-WndRect.top);
		org_y=pt.y;
		DrawXorBar(hdc,WndRect.left,y-clickoffset,WndRect.right-WndRect.left,WndRect.bottom-WndRect.top);
		//PatBlt(hdc,WndRect.left,WndRect.top,WndRect.right-WndRect.left,WndRect.bottom-WndRect.top,DSTINVERT);
		//FillRgn(hdc,m_region,GetSysColorBrush(COLOR_BACKGROUND));
		ReleaseDC(g_hwnd,hdc);

		
		InflateRect(&rect,0,-50);
		ClipCursor(&rect);
		SetCapture(g_hwnd);

		old_pt.x=x;
		old_pt.y=y;
	}
	else
		m_bmousedown=FALSE;


}
void PMSplitCtrl::MouseMove(DWORD fkey,DWORD x,DWORD y)
{
	HDC hdc;
	RECT rect;
	if(!CheckMouse(x,y))
		return;

	if(m_bmousedown)
	{
		
		SetRect(&rect,
			WndRect.left,
			0,/*old_pt.y-clickoffset-10,*/
			WndRect.right,
			(old_pt.y-clickoffset)+(WndRect.bottom-WndRect.top)+10);
		//RedrawWindow(g_hwnd,&rect,NULL,RDW_INVALIDATE|RDW_ERASE|RDW_UPDATENOW|RDW_ALLCHILDREN);
		//InvalidateRect(g_hwnd,&rect,TRUE);
		//UpdateWindow(g_hwnd);

		hdc=GetDC(g_hpwnd);
		//ClientToScreen( g_hwnd, &pt );
		//SetPixel(hdc,x,y+clickoffset,RGB(255,0,0));
		//PatBlt(hdc,WndRect.left,y-clickoffset+org_y,WndRect.right-WndRect.left,WndRect.bottom-WndRect.top,DSTINVERT);
		DrawXorBar(hdc,WndRect.left,old_pt.y-clickoffset+org_y,WndRect.right-WndRect.left,WndRect.bottom-WndRect.top);
		DrawXorBar(hdc,WndRect.left,y-clickoffset+org_y,WndRect.right-WndRect.left,WndRect.bottom-WndRect.top);

		ReleaseDC(g_hwnd,hdc);
	}
	old_pt.x=x;
	old_pt.y=y;
}

void PMSplitCtrl::MouseUp(DWORD fkey,DWORD x,DWORD y)
{
	RECT rect;
	HDC hdc;

	ReleaseCapture();
	//clean old, then draw new
	hdc=GetDC(g_hpwnd);
	DrawXorBar(hdc,WndRect.left,old_pt.y-clickoffset+org_y,WndRect.right-WndRect.left,WndRect.bottom-WndRect.top);
	//DrawXorBar(hdc,WndRect.left,y-clickoffset+org_y,WndRect.right-WndRect.left,WndRect.bottom-WndRect.top);
	ReleaseDC(g_hwnd,hdc);
	//
	OffsetRect(&WndRect,0,(old_pt.y-(clickoffset+WndRect.top)));
	GetWindowRect(g_hwnd,&rect);
	//notify main window to resize itself and children
	MoveWindow(g_hwnd,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,TRUE);
	m_bmousedown=FALSE;

}

void PMSplitCtrl::DrawXorBar(HDC hdc, int x1, int y1, int width, int height)
{
  static WORD _dotPatternBmp[8] = { 0x00aa, 0x0055, 0x00aa, 0x0055, 
                                    0x00aa, 0x0055, 0x00aa, 0x0055};

  HBITMAP hbm;
  HBRUSH  hbr, hbrushOld;

  /* create a monochrome checkered pattern */
  hbm = CreateBitmap(8, 8, 1, 1, _dotPatternBmp);

  hbr = CreatePatternBrush(hbm);
    
  SetBrushOrgEx(hdc, x1, y1, 0);
  hbrushOld = (HBRUSH)SelectObject(hdc, hbr);
    
  /* draw the checkered rectangle to the screen */
  PatBlt(hdc, x1, y1, width, height, PATINVERT);
    
  SelectObject(hdc, hbrushOld);
    
  DeleteObject(hbr);
  DeleteObject(hbm);
}